package com.example.opengldemo.simpleGame.util;

import com.bulletphysics.BulletGlobals;
import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.constraintsolver.SliderConstraint;
import com.bulletphysics.linearmath.MatrixUtil;
import com.bulletphysics.linearmath.Transform;
import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;
import com.example.opengldemo.simpleGame.addRigidBody.Claw;
import com.example.opengldemo.simpleGame.catcherFun.MySurfaceView;

import javax.vecmath.Vector3f;

import static com.example.opengldemo.simpleGame.constant.SourceConstant.*;

public class SliderHelper
{	
	CollisionShape boxShape;
	public static RigidBody cubeBody;
	SliderConstraint sliderUD;
	int TextureId;
    MySurfaceView viewManager;
    DiscreteDynamicsWorld dynamicsWorld;
	public SliderHelper(int TextureId,MySurfaceView viewManager,DiscreteDynamicsWorld dynamicsWorld)
	{
		this.TextureId=TextureId;
		this.viewManager=viewManager;
		this.dynamicsWorld=dynamicsWorld;
		initWorld();
	}
	public void initWorld()
	{
		boxShape=new BoxShape(new Vector3f(cubeSize,cubeSize/2,cubeSize));
		cubeBody = RigidBodyHelper.addRigidBody(0f,boxShape,0,8,12,dynamicsWorld,true);	//��Ӹ���

        Vector3f originA = new Vector3f(0, 0, 0);
 		Vector3f originB = new Vector3f(0, 0, 0);
		
		originA.set(0, 0, 0);
	    originB.set(0,ganULength/2, 0);
	 	addSliderConstraint(0,cubeBody, Claw.bodyg[0], BulletGlobals.SIMD_PI/2,originA,originB,true);//��ӳ�������צ��֮���Լ��
	}
	public void addSliderConstraint(int index,RigidBody ra,RigidBody rb,float angle,Vector3f originA,Vector3f originB,boolean force){
		Transform localA = new Transform();
		Transform localB = new Transform();
		localA.setIdentity();
		localB.setIdentity();
		MatrixUtil.setEulerZYX(localA.basis, 0, 0, angle);
		MatrixUtil.setEulerZYX(localB.basis, 0, 0, angle);
		localA.origin.set(originA);
		localB.origin.set(originB);
		sliderUD = new SliderConstraint(ra, rb, localA, localB, force);
		sliderUD.setLowerLinLimit(-2.8f);
		sliderUD.setUpperLinLimit(0f);
		sliderUD.setLowerAngLimit(0);
		sliderUD.setUpperAngLimit(0);
		sliderUD.setDampingDirAng(1.0f);
		sliderUD.setDampingDirLin(1f);
		dynamicsWorld.addConstraint(sliderUD,true);
		
	}
	public void slideUD(float mulFactor){
		sliderUD.getRigidBodyB().activate();
		sliderUD.setPoweredLinMotor(true);
		sliderUD.setMaxLinMotorForce(200.0f);
		sliderUD.setTargetLinMotorVelocity(20.0f*mulFactor);
	}
	public void drawSelf() {
		
         	MatrixState3D.pushMatrix();
         	Transform trans = cubeBody.getMotionState().getWorldTransform(new Transform());
			MatrixState3D.translate(trans.origin.x,trans.origin.y, trans.origin.z);
			trans.getOpenGLMatrix(MatrixState3D.getMMatrix());
			MatrixState3D.translate(0, -0.6f, 0);
         	ganbox.drawSelf(TextureId);
			MatrixState3D.popMatrix();
         
	}
}
